home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex6-3.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  978b  |  62 lines

  1. // ex6-3.c -- Order of construction of base and member classes
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex6-3.c,v 3.0 90/05/15 22:45:44 kgorlen Rel $
  4.  
  5. #include <iostream.h>
  6.  
  7. class X {
  8.     int i;
  9. public:
  10.     X(const char* s)    { cout << s << ' '; }
  11.     X()                 { cout << "X::X() "; }
  12. };
  13.  
  14. class A {
  15.     X a1;
  16.     X a2;
  17. public:
  18.     A(const char* s): a2("A::a2") { cout << s << ' '; }
  19. };
  20.  
  21. class B: public A {
  22.     X b1;
  23.     X b2;
  24. public:
  25.     B(const char* s):
  26.         b2("B::b2"),
  27.         b1("B::b1"),
  28.         A("B::A")   { cout << s << ' '; }
  29. };
  30.  
  31. int initCi()
  32. {
  33.     cout << "C::i ";
  34.     return 0;
  35. }
  36.  
  37. int& initCr()
  38. {
  39.     static int n = 1;
  40.     cout << "C::r ";
  41.     return n;
  42. }
  43.  
  44. class C: public B {
  45.     const int i;
  46.     int& r;
  47.     X c1;
  48.     X c2;
  49. public:
  50.     C(const char* s):
  51.         B("C::B"),
  52.         c1("C::c1"),
  53.         r(initCr()),
  54.         i(initCi()),
  55.         c2("C::c2") { cout << s << endl; }
  56. };
  57.  
  58. main()
  59. {
  60.     C c("c");
  61. }
  62.